<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * projekt CRUD model class 
 *
 * @author Gabor Bota <gab.bota at 3pixel.hu>
 */
Class Projekt_model extends CI_Model {

    public $projekt_table;

    function __construct() {
        // Call the Model constructor
        parent::__construct();
        $this->projekt_table = $this->db->dbprefix('projektek');
    }

    /**
     * 
     * 
     * 
     * Update projekt by id.
     * 
     * 
     * @return true on success
     */
    function update_projekt($projekt_data = array()) {
        // if projekt does not exists throw projekt_not_exists_exception
        if ($this->get_projekt($projekt_data['projekt_id']) == FALSE)
            throw new not_exists_exception("projekt");

        // do selection and update data
        $this->db->where('projekt_id', $projekt_data['projekt_id']);
        $this->db->update($this->projekt_table, $projekt_data);
    }

    /**
     * 
     * 
     * 
     * Delete projekt
     * 
     * 
     * 
     * @param type $id
     */
    function delete($projekt_id) {
        // delete projekt data from projekt_table
        $this->db->where('projekt_id', $projekt_id);
        $this->db->delete($this->projekt_table);
    }

    /**
     * 
     * Add new projekt
     * 
     * @param type $projekt_data
     */
    function add_projekt($projekt_data = array()) {
        // insert projekt_data
        $this->db->insert($this->projekt_table, $projekt_data);
    }

    /**
     * 
     * returns the projekt (for pagination)
     * 
     * options:
     *  select 
     *  search
     *  order_by
     *  order_dir
     *  limit
     *  offset
     * 
     * @param type $limit the number of returned projekt
     * @param type $offset from this number.
     * @return type 
     */
    function get_projektek($options = array()) {

        if (isset($options['select'])) {
            $this->db->select($options['select']);
        }
        
        if(isset($options['where'])){
            foreach ($options['where'] as $key => $value) {
                $this->db->where($key, $value);
            }
        }
        
        if (isset($options['search_like'])) {
            $this->db->like('projekt_nev', $options['search_like']);
            $this->db->or_like('projekt_leiras', $options['search_like']);
        }

        if (isset($options['limit']) && isset($options['offset']))
            $this->db->limit($options['limit'], $options['offset']);

        if (isset($options['order_by'])) {
            if (isset($options['order_dir'])) {
                $this->db->order_by($options['order_by'], $options['order_dir']);
            } else {
                $this->db->order_by($options['order_by']);
            }
        }

        $query = $this->db->get($this->projekt_table);


        return $query->result();
    }

    /**
     * 
     * Returns the number of projekt
     * 
     * 
     * @return type 
     */
    function count() {
        return $this->db->count_all_results($this->projekt_table);
    }

    /**
     * Gives back projekt by id 
     * 
     * @param type $id
     * @return array (projekt_id, projektname, projekt)
     */
    function get_projekt($id) {
        // constructing active record query
        $query = $this->db->get_where($this->projekt_table, array('projekt_id' => $id));
        // do the query
        $result = $query->result();
        if ($result == NULL)
            return NULL;
        return $result[0];
    }

    /**
     * Gives back projekt by id 
     * 
     * @param type $id
     * @return array (projekt_id, projektname, projekt)
     */
    function get_projekt_by_slug($slug, $ignore_id = NULL) {
        // constructing active record query
        // ignore id
        if ($ignore_id) {
            $this->db->where_not_in('projekt_id', $ignore_id);
        }
        $query = $this->db->get_where($this->projekt_table, array('projekt_nev_slug' => $slug));
        // do the query
        $result = $query->result();
        if ($result == NULL)
            return NULL;
        return $result[0];
    }

}